{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TRL"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This little example demonstrates how to use scikit-rf for a basic TRL calibration. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic Use \n",
    "\n",
    "### Imports \n",
    "\n",
    "First we import `skrf`, the `TRL` class, and setup matplotlib plotting. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import skrf as rf\n",
    "from skrf.calibration import TRL\n",
    "\n",
    "%matplotlib inline\n",
    "from skrf.plotting import stylely\n",
    "\n",
    "stylely()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  Load the data "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load the calibration standard's measurements and the switch terms from touchstone files. ( If you dont know what switch terms are, see [this section below](#Switch-Terms). )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "T = rf.Network('trl_data/thru.s2p')\n",
    "R = rf.Network('trl_data/reflect.s2p')\n",
    "L = rf.Network('trl_data/line.s2p')\n",
    "\n",
    "switch_terms = (rf.Network('trl_data/forward switch term.s1p'),\n",
    "                rf.Network('trl_data/reverse switch term.s1p'))\n",
    "\n",
    "measured = [T,R,L]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  Create a TRL calibration Object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "trl = TRL(measured = measured,\n",
    "          switch_terms = switch_terms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Correct a DUT with it. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dut_raw = rf.Network('trl_data/mismatched line.s2p')\n",
    "dut_corrected = trl.apply_cal(dut_raw)\n",
    "dut_corrected.plot_s_db()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Switch Terms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What are they?\n",
    "\n",
    "Described in [1] by Roger Marks, switch-terms account for the fact that the 8-term (aka error-box ) model is overly simplified. The two error networks change slightly depending on which port is being excited. This is due to the internal switch within the VNA. The change in reflection coefficient at the VNA's internal switch is known as a *switch term*, it  can be directed measured and removed.\n",
    "\n",
    "* [1] R.B. Marks, \"Formulations of the Basic Vector Network Analyzer Error Model including Switch-Terms,\" in ARFTG Conference Digest-Fall, 50th, 1997, vol. 32, pp. 115-126\n",
    "\n",
    "### How do I get them ?\n",
    "\n",
    "To measure the switch terms manually, you can define custom traces as ratio's of receivers and save the data to touchstone files. Assume ports are labeled 1 and 2, and incident waves are a's and reflected waves are b's. Then, the \n",
    "\n",
    "* **forward switch term** == `a2/b2` with `source port 1`\n",
    "* **reverse switch term** == `a1/b1` with `source port 2`\n",
    "\n",
    "\n",
    "\n",
    "### Can I get them with skrf? \n",
    "\n",
    "Yes. There is a PNA driver in the `skrf.vi.vna module`. It has code to pull the switch terms.\n",
    "\n",
    "### Can get them Onboard  a PNA?\n",
    "Yes. See the image below for an example of setting this up on a PNA.\n",
    "\n",
    "<img src=\"switch_term_dialog.png\" width=\"800\"/>"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
